home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- #
- # Graph.c
- #
- # Copyright © 1989-1990 Apple Computer, Inc.
- # All rights reserved.
- #
- # This file contains the library routines to support a simple graphing
- # algorithm.
- #
- ------------------------------------------------------------------------------*/
-
- /* The list of includes will be limited to those that are available with every
- C compiler. So, even though this engine is written in MPW, we'll limit the list
- of includes to just those files that are common across development environments */
-
-
- #include <StdDef.h> /* common definitions */
- #include <StdLib.h> /* General utilities */
- #include <Graph.h> /* our graphics engine stuff */
-
- GraphStructPtr DoGraphInit( GraphType whichGraphType )
- {
- GraphStructPtr graphStorage = 0;
- short counter;
- GraphValue aGraphValue;
-
- switch ( whichGraphType ) {
- case kBar:
- if (!(graphStorage = (GraphStructPtr) malloc( sizeof (GraphStruct))))
- return 0; /* error... */
- graphStorage->numPoints = graphStorage->top = graphStorage->left =
- graphStorage->bottom = graphStorage->right =
- graphStorage->graphYMax = graphStorage->graphYMin = 0;
- for (counter = 0; counter < kMaxPoints; counter++ ) {
- aGraphValue = graphStorage->graphItems[counter];
- aGraphValue.whichOne = aGraphValue.value =
- aGraphValue.top = aGraphValue.left =
- aGraphValue.right = aGraphValue.bottom = 0;
- }
- break;
- case kStackedBar:
- case kPie:
- case kLine:
- /* these are unsupported in this version */
- break;
- }
- return graphStorage;
- }
-
- void DoGraphSetGraphRect( short top, short left, short bottom, short right,
- GraphStructPtr graphStorage )
- {
- graphStorage->top = top;
- graphStorage->left = left;
- graphStorage->bottom = bottom;
- graphStorage->right = right;
- }
-
- void DoGraphSetPoint( short which, long value, GraphStructPtr graphStorage )
- {
- if ((which < 1) || (which > kMaxPoints))
- return; /* can't set this point - out of range */
-
- if (which > graphStorage->numPoints)
- graphStorage->numPoints = which; /* assign a new number of points */
-
- if (value > graphStorage->graphYMax)
- graphStorage->graphYMax = value; /* assign a new maximum */
-
- if (value < graphStorage->graphYMin)
- graphStorage->graphYMin = value; /* assign a new minimum */
-
- graphStorage->graphItems[which - 1].whichOne = which;
- graphStorage->graphItems[which - 1].value = value;
- }
-
- short DoGraphGetNumPoints( GraphStructPtr graphStorage )
- {
- return graphStorage->numPoints;
- }
-
- void DoGraphComputeBars( GraphStructPtr graphStorage )
- /* NOTE assumes the following graphics coordinates: 0,0 at topleft */
- {
- short i,graphWidth,graphHeight,yRange;
- GraphValue aGraphValue;
-
- graphWidth = (graphStorage->right - graphStorage->left)
- / graphStorage->numPoints;
- graphHeight = graphStorage->bottom - graphStorage->top;
- yRange = graphStorage->graphYMax - graphStorage->graphYMin;
- for (i = 0; i < graphStorage->numPoints; i++) {
- aGraphValue = graphStorage->graphItems[i];
- aGraphValue.left = graphStorage->left + (i * graphWidth);
- aGraphValue.right = aGraphValue.left + graphWidth;
- aGraphValue.bottom = graphStorage->bottom;
- aGraphValue.top = graphStorage->bottom -
- (graphHeight * (aGraphValue.value - graphStorage->graphYMin) / yRange);
- aGraphValue.left += kMargin;
- graphStorage->graphItems[i] = aGraphValue;
- }
- }
-
- short DoGraphGetYMax( GraphStructPtr graphStorage )
- {
- return graphStorage->graphYMax;
- }
-
- short DoGraphGetYMin( GraphStructPtr graphStorage )
- {
- return graphStorage->graphYMin;
- }
-
- short DoGraphGetBar( short which, short *top, short *left, short *bottom, short *right,
- GraphStructPtr graphStorage )
- /* DoGraphGetBar assigns the dimensions of the bar for this point in the bar chart and also
- returns the value of this bar */
- {
- GraphValue aGraphValue;
-
- aGraphValue = graphStorage->graphItems[which-1];
- if (aGraphValue.whichOne != which)
- return 0;
- else {
- *top = aGraphValue.top;
- *left = aGraphValue.left;
- *bottom = aGraphValue.bottom;
- *right = aGraphValue.right;
- return aGraphValue.value;
- }
- }
-
- GraphStructPtr DoGraphDispose( GraphStructPtr graphStorage )
- {
- free(graphStorage);
- return NULL;
- }
-